﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarolLib.Control;
using System.Xml;
using System.Collections;
using DispatchAssist.Service;
using System.Threading;
using CarolLib;
using DispatchAssist.Model;

namespace DispatchAssist.AppHost
{

    [ModuleContract("B5304268-5A44-4596-9994-BC7889A9620B", "系统管理", "委托席位")]
    public partial class ucDelegateManage : MdiUserControl
    {

        #region members

        private AlertMessageGetterService serviceProxy;

        List<Seat> dataList;

        #endregion


        #region sys
        public ucDelegateManage()
               :base()
        {
            InitializeComponent();
        }

        public ucDelegateManage(XmlNode node)
            : this()
        {

        }

        public override void Onload()
        {
            base.Onload();

            serviceProxy = new AlertMessageGetterService();

            serviceProxy.GetDelegatableSeatsCompleted+=new GetDelegatableSeatsCompletedEventHandler(serviceProxy_GetDelegatableSeatsCompleted);
            
            serviceProxy.InsertDelegateSeatCompleted+=new InsertDelegateSeatCompletedEventHandler(serviceProxy_InsertDelegateSeatCompleted);

            serviceProxy.GetDelegatableSeatsAsync(AuthContext.Current.SelfSeat.SeatID);
        }



        public override void Unload()
        {     
            base.Unload();
            serviceProxy = null;
            dataList = null;
        }

        private void btnSure_Click(object sender, EventArgs e)
        {
            if (ddlSeatList.SelectedIndex == 0)
            {
                MessageBox.Show("请选择席位");
                return;
            }

           Service.SeatRelation model=new Service.SeatRelation();

            model.ApplyUserID=AuthContext.Current.User.UserID;
            model.SelfSeatID=(Guid)ddlSeatList.SelectedValue;
            model.DelegateSeatID=AuthContext.Current.SelfSeat.SeatID;
            model.ConfirmStatus=0;
            model.Comment=txtComment.Text;

            serviceProxy.InsertDelegateSeatAsync(model);

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.ClearData();
        }

        #endregion

        #region webservice sync complete

        private void serviceProxy_GetDelegatableSeatsCompleted(object sender, GetDelegatableSeatsCompletedEventArgs e)
        {
            try
            {
               dataList = e.Result.ToList<Seat>();
            }
            catch
            {
                dataList = new List<Seat>();
            }

            dataList.Insert(0, new Seat() { SeatID=Guid.Empty,SeatName="--请选择--"});

            ddlSeatList.DisplayMember = "SeatName";
            ddlSeatList.ValueMember = "SeatID";

            ddlSeatList.DataSource = dataList;

        }

        private void serviceProxy_InsertDelegateSeatCompleted(object sender, InsertDelegateSeatCompletedEventArgs e)
        {
            if (string.IsNullOrEmpty(e.Result))
            {
                MessageBox.Show("委托请求发送成功,等待对方应答");

                ClearData();
            }
            else
            {
                MessageBox.Show(e.Result);
            }
        }

        #endregion

        #region customer method

        private void ClearData()
        {
            ddlSeatList.SelectedIndex = 0;
            txtComment.Text = "";
        }


        #endregion

        

    }
}
